package com.rogue.blog.service.article.impl;

import com.rogue.blog.dao.article.ArticleDao;
import com.rogue.blog.model.dto.article.ArticleDTO;
import com.rogue.blog.model.po.PageBean;
import com.rogue.blog.model.po.article.ArticlePO;
import com.rogue.blog.model.vo.article.ArticleCardVO;
import com.rogue.blog.model.vo.article.ArticleVO;
import com.rogue.blog.service.article.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * Created by 86101 on 2018-10-22.
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    // TODO
    private long userId = 4137048204575744L;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ArticleDao articleDao;

    @Override
    public long insert(ArticleDTO dto) {
        List<ArticlePO> pos = articleDao.selectArticleByNameAndUserId(dto.getArticleName(), -1, userId);
        long articleId;
        if (CollectionUtils.isEmpty(pos)) {
            ArticlePO po = dto.toPO();
            articleId = restTemplate.getForObject("http://rogue-generator-service/nextId", Long.class);
            po.setArticleId(articleId);
            po.setUserId(userId);
            po.setCreateUserId(userId);
            po.setUpdateUserId(userId);
            int row = articleDao.insert(po);
            if (0 == row) {
                articleId = -1;
            }
        } else {
            articleId = pos.get(0).getArticleId();
        }
        return articleId;
    }

    @Override
    public int deleteById(long id, boolean hardDelete) {
        ArticlePO po = articleDao.selectById(id);
        if (userId == po.getUserId()) {
            if (hardDelete) {
                return articleDao.deleteById(id);
            } else {
                po.setValid("0");
                po.setUpdateUserId(userId);
                return articleDao.updateById(po);
            }
        } else {
            return 0;
        }
    }

    @Override
    public int updateById(long id, ArticleDTO dto) {
        ArticlePO po = articleDao.selectById(id);
        if (null == po) {
            return 0;
        } else {
            po = dto.toPO();
            po.setArticleId(id);
            po.setUpdateUserId(userId);
            return articleDao.updateById(po);
        }
    }

    @Override
    public ArticleVO selectById(long id) {
        ArticlePO po = articleDao.selectById(id);
        ArticleVO vo = null;
        if (null != po) {
            vo = po.toVO();
        }
        return vo;
    }

    @Override
    public PageBean<ArticleCardVO> selectByPage(int currentPage, int pageSize, String search) {
        int start = (currentPage - 1) * pageSize;
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("userId", userId);
        pageMap.put("start", start);
        pageMap.put("limit", pageSize);
        pageMap.put("search", search);
        List<ArticleCardVO> vos = articleDao.selectByPage(pageMap);
        int totalCount = articleDao.selectCountByPage(pageMap);
        PageBean<ArticleCardVO> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setPageData(vos);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    @Override
    public boolean exists(String articleName, long articleId) {
        return !CollectionUtils.isEmpty(articleDao.selectArticleByNameAndUserId(articleName, articleId, userId));
    }
}
